home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / externaltools / functions.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  7.4 KB  |  202 lines

  1. # -*- coding: utf-8 -*-
  2. #    Gedit External Tools plugin
  3. #    Copyright (C) 2005-2006  Steve Fr√©cinaux <steve@istique.net>
  4. #
  5. #    This program is free software; you can redistribute it and/or modify
  6. #    it under the terms of the GNU General Public License as published by
  7. #    the Free Software Foundation; either version 2 of the License, or
  8. #    (at your option) any later version.
  9. #
  10. #    This program is distributed in the hope that it will be useful,
  11. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #    GNU General Public License for more details.
  14. #
  15. #    You should have received a copy of the GNU General Public License
  16. #    along with this program; if not, write to the Free Software
  17. #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18.  
  19. import os
  20. import gtk
  21. from gtk import gdk
  22. import gio
  23. import gedit
  24. #import gtksourceview
  25. from outputpanel import OutputPanel
  26. from capture import *
  27.  
  28. def default(val, d):
  29.     if val is not None:
  30.         return val
  31.     else:
  32.         return d
  33.  
  34. # ==== Capture related functions ====
  35. def capture_menu_action(action, window, node):
  36.  
  37.     # Get the panel
  38.     panel = window.get_data("ExternalToolsPluginWindowData")._output_buffer
  39.     panel.show()
  40.     panel.clear()
  41.  
  42.     # Configure capture environment
  43.     try:
  44.         cwd = os.getcwd()
  45.     except OSError:
  46.         cwd = os.getenv('HOME');
  47.  
  48.     capture = Capture(node.command, cwd)
  49.     capture.env = os.environ.copy()
  50.     capture.set_env(GEDIT_CWD = cwd)
  51.  
  52.     view = window.get_active_view()
  53.     if view is not None:
  54.         # Environment vars relative to current document
  55.         document = view.get_buffer()
  56.         uri = document.get_uri()
  57.         if uri is not None:
  58.             gfile = gio.File(uri)
  59.             scheme = gfile.get_uri_scheme()
  60.             name = os.path.basename(uri)
  61.             capture.set_env(GEDIT_CURRENT_DOCUMENT_URI    = uri,
  62.                             GEDIT_CURRENT_DOCUMENT_NAME   = name,
  63.                             GEDIT_CURRENT_DOCUMENT_SCHEME = scheme)
  64.             if gedit.utils.uri_has_file_scheme(uri):
  65.                 path = gfile.get_path()
  66.                 cwd = os.path.dirname(path)
  67.                 capture.set_cwd(cwd)
  68.                 capture.set_env(GEDIT_CURRENT_DOCUMENT_PATH = path,
  69.                                 GEDIT_CURRENT_DOCUMENT_DIR  = cwd)
  70.  
  71.         documents_uri = [doc.get_uri()
  72.                                  for doc in window.get_documents()
  73.                                  if doc.get_uri() is not None]
  74.         documents_path = [gio.File(uri).get_path()
  75.                                  for uri in documents_uri
  76.                                  if gedit.utils.uri_has_file_scheme(uri)]
  77.         capture.set_env(GEDIT_DOCUMENTS_URI  = ' '.join(documents_uri),
  78.                         GEDIT_DOCUMENTS_PATH = ' '.join(documents_path))
  79.  
  80.     capture.set_flags(capture.CAPTURE_BOTH)
  81.  
  82.     # Assign the error output to the output panel
  83.     panel.process = capture
  84.  
  85.     # Get input text
  86.     input_type = node.input
  87.     output_type = node.output
  88.  
  89.     if input_type != 'nothing' and view is not None:
  90.         if input_type == 'document':
  91.             start, end = document.get_bounds()
  92.         elif input_type == 'selection':
  93.             try:
  94.                 start, end = document.get_selection_bounds()
  95.             except ValueError:
  96.                 start, end = document.get_bounds()
  97.                 if output_type == 'replace-selection':
  98.                     document.select_range(start, end)
  99.         elif input_type == 'line':
  100.             start = document.get_iter_at_mark(document.get_insert())
  101.             end = start.copy()
  102.             if not start.starts_line():
  103.                 start.set_line_offset(0)
  104.             if not end.ends_line():
  105.                 end.forward_to_line_end()
  106.         elif input_type == 'word':
  107.             start = document.get_iter_at_mark(document.get_insert())
  108.             end = start.copy()
  109.             if not start.inside_word():
  110.                 panel.write(_('You must be inside a word to run this command'),
  111.                             panel.command_tag)
  112.                 return
  113.             if not start.starts_word():
  114.                 start.backward_word_start()
  115.             if not end.ends_word():
  116.                 end.forward_word_end()
  117.  
  118.         input_text = document.get_text(start, end)
  119.         capture.set_input(input_text)
  120.  
  121.     # Assign the standard output to the chosen "file"
  122.     if output_type == 'new-document':
  123.         tab = window.create_tab(True)
  124.         view = tab.get_view()
  125.         document = tab.get_document()
  126.         pos = document.get_start_iter()
  127.         capture.connect('stdout-line', capture_stdout_line_document, document, pos)
  128.         document.begin_user_action()
  129.         view.set_editable(False)
  130.         view.set_cursor_visible(False)
  131.     elif output_type != 'output-panel' and view is not None:
  132.         document.begin_user_action()
  133.         view.set_editable(False)
  134.         view.set_cursor_visible(False)
  135.  
  136.         if output_type == 'insert':
  137.             pos = document.get_iter_at_mark(document.get_mark('insert'))
  138.         elif output_type == 'replace-selection':
  139.             document.delete_selection(False, False)
  140.             pos = document.get_iter_at_mark(document.get_mark('insert'))
  141.         elif output_type == 'replace-document':
  142.             document.set_text('')
  143.             pos = document.get_end_iter()
  144.         else:
  145.             pos = document.get_end_iter()
  146.         capture.connect('stdout-line', capture_stdout_line_document, document, pos)
  147.     else:
  148.         capture.connect('stdout-line', capture_stdout_line_panel, panel)
  149.         document.begin_user_action()
  150.  
  151.     capture.connect('stderr-line',   capture_stderr_line_panel,   panel)
  152.     capture.connect('begin-execute', capture_begin_execute_panel, panel, node.name)
  153.     capture.connect('end-execute',   capture_end_execute_panel,   panel, view,
  154.                     output_type in ('new-document','replace-document'))
  155.  
  156.     # Run the command
  157.     view.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gdk.Cursor(gdk.WATCH))
  158.     capture.execute()
  159.     document.end_user_action()
  160.  
  161. def capture_stderr_line_panel(capture, line, panel):
  162.     panel.write(line, panel.error_tag)
  163.  
  164. def capture_begin_execute_panel(capture, panel, label):
  165.     panel['stop'].set_sensitive(True)
  166.     panel.clear()
  167.     panel.write(_("Running tool:"), panel.italic_tag);
  168.     panel.write(" %s\n\n" % label, panel.bold_tag);
  169.  
  170. def capture_end_execute_panel(capture, exit_code, panel, view, update_type):
  171.     panel['stop'].set_sensitive(False)
  172.  
  173.     if update_type:
  174.         doc = view.get_buffer()
  175.         start = doc.get_start_iter()
  176.         end = start.copy()
  177.         end.forward_chars(300)
  178.  
  179.         mtype = gio.content_type_guess(data=doc.get_text(start, end))
  180.         lmanager = gedit.get_language_manager()
  181.         language = gedit.language_manager_get_language_from_mime_type(lmanager, mtype)
  182.         if language is not None:
  183.             doc.set_language(language)
  184.  
  185.     view.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gdk.Cursor(gdk.XTERM))
  186.     view.set_cursor_visible(True)
  187.     view.set_editable(True)
  188.  
  189.     if exit_code == 0:
  190.         panel.write("\n" + _("Done.") + "\n", panel.italic_tag)
  191.     else:
  192.         panel.write("\n" + _("Exited") + ":", panel.italic_tag)
  193.         panel.write(" %d\n" % exit_code, panel.bold_tag)
  194.  
  195. def capture_stdout_line_panel(capture, line, panel):
  196.     panel.write(line)
  197.  
  198. def capture_stdout_line_document(capture, line, document, pos):
  199.     document.insert(pos, line)
  200.  
  201. # ex:ts=4:et:
  202.